using System;
using System.Collections.Generic;

namespace Mbs.Elements
{
	[Serializable]
    class ElementCategoryRepository : Dictionary<string, IElementCategory>, IElementCategoryRepository
	{
        IElementCategory IElementCategoryRepository.this[string siteName]
		{
			get
			{
				if (ContainsKey(siteName))
					return base[siteName];
                throw new ArgumentException(String.Format("not exists the site name:{0}", siteName));
			}
		}

		public void RegisterSite(string siteName, object uiElement)
		{
            if (string.IsNullOrEmpty(siteName))
                throw new ArgumentNullException("siteName");
            if (uiElement == null)
                throw new ArgumentNullException("uiElement");

            IElement element = ServiceLocator.Current.Get<IElementService>().ElementMap.Register(uiElement);
            RegisterSite(siteName, element);
		}


        private void RegisterSite(string siteName, IElement element)
		{
            if (element == null)
                throw new ArgumentNullException("element");

			if (ContainsKey(siteName))
                throw new ArgumentException(String.Format("Duplicate ui extension site:{0}", siteName));

            Add(siteName, new ElementCategory(element));
		}

		public void UnregisterSite(string siteName)
		{
            if (string.IsNullOrEmpty(siteName))
                throw new ArgumentNullException("siteName");

			Remove(siteName);
		}

		public void Dispose()
		{
			Dispose(true);
			GC.SuppressFinalize(this);
		}

		protected virtual void Dispose(bool disposing)
		{
			foreach (ElementCategory site in Values)
				site.Clear();

			Clear();
		}
	}
}
